﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using Nissen_Model;

namespace Nissen_Dao
{
	public class WorkerCacheDao : DaoBase
	{
		public WorkerCache GetWorkerCacheBySSO(string sso)
		{
			using (ISession session = this.SessionFactory.OpenSession())
			{
				ICriteria crt = session.CreateCriteria<WorkerCache>();

				crt.Add(Restrictions.Eq("WorkerSSO", sso));

				IList<WorkerCache> workerCaches = crt.List<WorkerCache>();


				if (workerCaches.Count>0)
				{
					return workerCaches[0];
				}
				return null;
			}
		}

		public void SaveOrUpdateWorkerCache(WorkerCache workerCache)
		{
			using (ISession session = this.SessionFactory.OpenSession())
			{
				using (ITransaction t1 = session.BeginTransaction())
				{

					ICriteria crt = session.CreateCriteria<WorkerCache>();

					crt.Add(Restrictions.Eq("WorkerSSO", workerCache.WorkerSSO));

					IList<WorkerCache> workerCaches = crt.List<WorkerCache>();


					if (workerCaches.Count > 0)
					{
						workerCaches[0].WorkerName = workerCache.WorkerName;
						session.SaveOrUpdate(workerCaches[0]);
					}
					else
					{
						session.SaveOrUpdate(workerCache);
					}
					t1.Commit();
				}

			}
		}
	}
}
